import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../../constants';
import {PERMISSION} from '../constants';
import {baseDataStatus} from '../../Enum';
import TextInput from 'Shared/components/TextInput';
import {hasPermissions} from '../utils';
import routes from '../routes';
import {injectIntl, language} from '../intl';
import WrappedSelect from '../../common/WrappedSelect';
const FormItem = Form.Item;
const baseDataStatusList = baseDataStatus.toList(language);
class NoticeProgramQueryPanel extends React.PureComponent {
    state = {
        data: {}
    }

    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    render() {
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.add))
            operations.push(<Button key="add" type="primary" href={`#${routes.noticeProgramAdd.url()}`}>{this.props.intl.formatMessage({
                id: 'noticeProgramQueryPanel.operation.add',
                defaultMessage: '新增'
            })}</Button>);
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'noticeProgramQueryPanel.form.code',
                                defaultMessage: '编号'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="code"
                                value={this.props.conditions.code}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'noticeProgramQueryPanel.form.name',
                                defaultMessage: '名称'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextInput
                                name="name"
                                value={this.props.conditions.name}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'noticeProgramQueryPanel.form.status',
                                defaultMessage: '状态'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                allowClear
                                name="status"
                                value={this.props.conditions.status}
                                options={baseDataStatusList}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                            {this.props.intl.formatMessage({
                                id: 'noticeProgramQueryPanel.btn.query',
                                defaultMessage: '查询'
                            })}
                        </Button>
                        <Button key="clear" onClick={this.props.onResetClick}>
                            {this.props.intl.formatMessage({
                                id: 'noticeProgramQueryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}
                        </Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        {operations}
                    </Col>
                </Row>
            </Form>
        );
    }
}

NoticeProgramQueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveNoticeProgramCondition,
    resetNoticeProgramQueryPanel,
    queryNoticeProgramSearch,
} from '../actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'noticeProgramCondition']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'programList', 'isFetching'])),
    conditions: getConditions(state),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveNoticeProgramCondition(name, value)),
    querySearch: () => dispatch(queryNoticeProgramSearch()),
    onResetClick: () => dispatch(resetNoticeProgramQueryPanel()),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(NoticeProgramQueryPanel));
